;;; init-dired.el --- Dired mode configuration -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:

(with-eval-after-load 'dired
  (require 'dired-x)

  (defun ctrlwei/dired-up-directory()
    "Goto up directory and resue buffer"
    (interactive)
    (find-alternate-file ".."))

  (defun ctrlwei/dired-sort ()
    "Sort dired dir listing in different ways.
Prompt for a choice.
Origin source at
URL `http://ergoemacs.org/emacs/dired_sort.html'"
    (interactive)
    (let ($sort-by $arg)
      (setq $sort-by (ido-completing-read "Sort by:" '( "date" "size" "name" )))
      (cond
       ((equal $sort-by "name") (setq $arg "-Alh \"--time-style=+%y/%m/%d|%W-%w %H:%M\""))
       ((equal $sort-by "date") (setq $arg "-Alht \"--time-style=+%y/%m/%d|%W-%w %H:%M\""))
       ((equal $sort-by "size") (setq $arg "-AlhS \"--time-style=+%y/%m/%d|%W-%w %H:%M\""))
       ;; ((equal $sort-by "dir") (setq $arg "-Al --group-directories-first"))
       (t (error "logic error 09535" )))
      (dired-sort-other $arg )))

  (define-key dired-mode-map (kbd "a") #'dired-find-file)
  (define-key dired-mode-map (kbd ")") #'dired-omit-mode)
  (define-key dired-mode-map (kbd "C-k") #'ctrlwei/dired-up-directory)
  (define-key dired-mode-map (kbd "q") #'kill-current-buffer)
  (define-key dired-mode-map (kbd "s") #'ctrlwei/dired-sort)
  (setq dired-recursive-copies 'always         ; allow dired copy dir without ask confirm
        dired-recursive-deletes 'always        ; allow dired delete  dir without ask confirm
        dired-dwim-target t                    ; copy from one dired dir to the next dired dir shown in a split window
        wdired-allow-to-change-permissions t   ; allow editing file permissions
        dired-omit-files (concat dired-omit-files "\\|^\\..+$\\|\\.pyc$|^__pycache__$")
        dired-listing-switches "-alh \"--time-style=+%y/%m/%d|%W-%u %H:%M\""
        dired-kill-when-opening-new-dired-buffer t)
  (add-hook 'dired-mode-hook 'dired-omit-mode))

(provide 'init-dired)
;;; init-dired.el ends here
